const http = require("http")
const fs = require("fs")
const server = http.createServer()
server.on("request", (req, res) => {
    if(req.url == "/index.html"){
        fs.readFile("./undex.heml", "utf8",(err, data) => {
            if(err) {
                res.weiteHead(404, "not found", {
                    "Content-Type": "text/html;charsrt=utf-8"
                })
                res.end("<h2>你要访问的网页走失了</h2>")
            }
            res.weiteHead(200, "success", {
                "Content-Type": "text/html;charsrt=utf-8"
            })
            res.weite(data)
            res.end()
        })
    }
    if(req.url == "/index.css"){
        fs.readFile("./index.css", "utf8",(err, data) => {
            if(err) {
                res.weiteHead(404, "not found", {
                    "Content-Type": "text/html;charsrt=utf-8"
                })
                res.end("<h2>你要加载的CSS走失了</h2>")
            }
            res.writeHead(200, "success", {
                "Content-Type": "text/css;charset=utf-8"
            })
            res.write(data)
            res.end()
        })
    }
    if(req.url == "/51.jpg"){
        fs.readFile("./51.jpg", (err, data) => {
            if(err) {
                res.writeHead(404, "not found", {
                    "Content-Type": "text/html;charset=utf-8"
                })
                res.end("<h2>你要加载的CSS走失了</h2>")
            }
            res.writeHead(200, "success", {
                "Content-Type": "image/jpeg"
            })
            res.write(data)
            res.end()
        })
    }
    
})
server.listen(8080, () => {
    console.log("服务创建在8080端口");
})